package com.babble.subitem.exception;

import org.springblade.common.exception.MsgException;
import org.springblade.core.tool.api.R;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
	@ExceptionHandler(value = MsgException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public R exceptionHandler(HttpServletRequest request, Exception e) {
		//绑定异常是需要明确提示给用户的
		if (e instanceof MsgException) {
			MsgException exception = (MsgException) e;
			return R.fail(exception.getCode(), exception.getMessage());//将具体错误信息设置到CodeMsg中返回
		}
		// 其余异常简单返回为服务器异常
		return R.fail(e.getMessage());
	}
}

